package hr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hr.entity.DO.EmployeeProbationReviewDO;
import hr.entity.Employee;
import hr.entity.Probationreview;
import hr.entity.vo.EmployeeProbationVo;
import hr.entity.vo.ProbationReviewVo;
import hr.mapper.EmployeeOnboardingFormMapper;
import hr.mapper.EmployeesMapper;
import hr.mapper.ProbationreviewMapper;
import hr.service.EmployeesService;
import hr.service.ProbationreviewService;
import hr.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 试用期审查表 服务实现类
 * </p>
 *
 * @author zzh
 * @since 2023-12-10
 */
@Service
public class ProbationreviewServiceImpl extends ServiceImpl<ProbationreviewMapper, Probationreview> implements ProbationreviewService {
    @Autowired
    private ProbationreviewMapper probationreviewMapper;

    @Autowired
    private EmployeesMapper employeesMapper;

    private String handleSql(EmployeeProbationVo employeeProbationVo) {
        Boolean isTurned = employeeProbationVo.getTurned();// 获取“转正”状态
        Boolean isRejected = employeeProbationVo.getRejected();// 获取“不通过”状态
        Boolean isDelayed = employeeProbationVo.getDelayed();// 获取“延期”状态
        String sql = null;
        if (isTurned != null && isTurned) {
            sql = "AND (sees.id = 7";
        }
        if (isRejected != null && isRejected) {
            if (sql != null) {
                sql += " OR sees.id = 8";
            } else {
                sql = "AND (sees.id = 8";
            }
        }
        if (isDelayed != null && isDelayed) {
            if (sql != null) {
                sql += " OR sees.id = 9";
            } else {
                sql = "AND (sees.id = 9";
            }
        }
        if (sql != null) {
            sql += ")";
        }
        return sql;
    }

    //获取所有数据
    @Override
    public List<EmployeeProbationReviewDO> getAll(EmployeeProbationVo employeeProbationVo) {
        long offset = (employeeProbationVo.getPage() - 1) * employeeProbationVo.getLimit();
        // 处理动态SQL
        List<EmployeeProbationReviewDO> employeeProbationReviewDOS = probationreviewMapper.getAll(
                employeeProbationVo.getName(),
                employeeProbationVo.getTurned(), // 获取“转正”状态
                employeeProbationVo.getRejected(), // 获取“不通过”状态
                employeeProbationVo.getDelayed(), // 获取“延期”状态
                offset,
                employeeProbationVo.getLimit(),
                handleSql(employeeProbationVo));
        employeeProbationReviewDOS.forEach(e -> {
            e.setJoinDateStr(CommonUtils.convertTimestampToYMDAndTime(e.getJoinDate()));
        });

        return employeeProbationReviewDOS;
    }

    //删除试用期员工
    @Override
    public boolean deleteByProbationId(Integer probationId) throws Exception {
        // 检查是否有 '转正' 状态的记录
        QueryWrapper<Probationreview> wrapper = new QueryWrapper<>();
        wrapper.eq("probation_id", probationId)
                .eq("probation_status", "转正");

        Probationreview probationReview = probationreviewMapper.selectOne(wrapper);
        if (probationReview != null) {
            throw new Exception("转正状态的记录无法删除");
        }

        // 进行删除操作
        int deletedRows = probationreviewMapper.deleteByProbationId(probationId);
        if (deletedRows > 0) {
            return true;
        } else {
            throw new Exception("删除操作未成功");
        }
    }

    //修改员工试用期状态
    @Override
    @Transactional //事务支持
    public boolean updateProbationStatus(ProbationReviewVo vo) {
        Long employeeId = vo.getEmployeeId();
        Integer employeeState = vo.getEmployeeState();

        Employee existingEmployee = employeesMapper.selectById(employeeId);

        if (existingEmployee != null) {
            // 更新员工的状态
            existingEmployee.setEmployeeState(employeeState);
            int updatedRows = employeesMapper.updateByEmployeeId(existingEmployee);

            return updatedRows > 0;
        } else {
            return false;
        }
    }

    @Override
    public List<EmployeeProbationReviewDO> getProbationReviewExcel() {
        List<EmployeeProbationReviewDO> employeeProbationReviewDOS = probationreviewMapper.getProbationReviewExcel();
        return employeeProbationReviewDOS;
    }

    @Override
    public long getAllCount(EmployeeProbationVo employeeProbationVo) {
        return probationreviewMapper.getAllCount(
                employeeProbationVo.getName(),
                handleSql(employeeProbationVo));
    }


}
